home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / yacas_alg / yacas_morphos / share / yacas / include / lispobject.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  4.1 KB  |  158 lines

  1. /** \file lispobject.h
  2.  *  Implementation of basic LispObject, which is the base class for
  3.  *  anything that can be put in a linked list.
  4.  *
  5.  * class LispObject. This class implements one lisp object, which is a
  6.  * abstract class containing reference counting and a next pointer.
  7.  * derive from this to implement specific types of list elements.
  8.  * The local class LispPtr implements automatic garbage collection
  9.  * through reference counting.
  10.  *
  11.  */
  12.  
  13.  
  14. #ifndef __lispobject_h__
  15. #define __lispobject_h__
  16.  
  17. #include "yacasbase.h"
  18. #include "refcount.h"
  19. #include "lispstring.h"
  20. #include "genericobject.h"
  21. #include "evalfunc.h"
  22.  
  23.  
  24. #ifdef YACAS_DEBUG
  25. void IncNrObjects();
  26. void DecNrObjects();
  27. #endif
  28.  
  29. class LispObject;
  30.  
  31. /** class LispPtr. This class is a smart pointer type class to Lisp
  32.  *  objects that can be inserted into linked lists. They do the actual
  33.  *  reference counting, and consequent destruction of the object if
  34.  *  nothing points to it. LispPtr is used in LispObject as a pointer
  35.  *  to the next object, and in diverse parts of the built-in internal
  36.  *  functions to hold temporary values.
  37.  */
  38. class LispPtr : public YacasBase
  39. {
  40. public:
  41.     inline LispPtr();
  42.     inline LispPtr(LispPtr& aOther);
  43.     inline LispPtr(LispObject& aOther);
  44.     inline ~LispPtr();
  45.     LispPtr& operator=(LispPtr& aOther);
  46.     LispPtr& operator=(LispObject& aOther);
  47.     inline void Set(LispObject* aNext);
  48.     inline LispObject* Get() const;
  49.     // For use as an iterator
  50.     inline LispObject* operator()();
  51.     inline void GoNext();
  52.  
  53. private:
  54.     inline void DestroyPrevious();
  55.     inline void DoSet(LispObject* aNext);
  56.     
  57. private:
  58.     LispObject* iNext;
  59. };
  60.  
  61.  
  62.  
  63. /** class LispObject is the base object class that can be put in
  64.  *  linked lists. It either has a pointer to a string, obtained through
  65.  *  String(), or it is a holder for a sublist, obtainable through SubList(),
  66.  *  or it is a generic object, in which case Generic() returns non-NULL.
  67.  *  Only one of these three functions should return a non-NULL value.
  68.  *  It is a reference-counted object. LispPtr handles the reference counting.
  69.  */
  70. class LispObject : public RefCountedObject
  71. {
  72. public:
  73.     inline LispPtr& Next();
  74. public: //Derivables
  75.     virtual ~LispObject();
  76.  
  77.     /** Return string representation, or NULL if the object doesn't have one.
  78.      *  the string representation is only relevant if the object is a
  79.      *  simple atom. This method returns NULL by default.
  80.      */
  81.     virtual LispStringPtr String() const;
  82.     /** If this object is a list, return a pointer to it.
  83.      *  Default behaviour is to return NULL.
  84.      */
  85.     virtual LispPtr* SubList();
  86.     virtual GenericClass* Generic();
  87.     virtual EvalFuncBase* EvalFunc();
  88.     virtual void SetEvalFunc(EvalFuncBase* aEvalFunc);
  89.     virtual LispObject* Copy(LispInt aRecursed)=0;
  90.  
  91.     /** Return a pointer to extra info. This allows for annotating
  92.      *  an object. Returns NULL by default.
  93.      */
  94.     virtual LispPtr* ExtraInfo();
  95.     virtual LispObject* SetExtraInfo(LispPtr& aData) = 0;
  96. public:
  97.     LispInt Equal(LispObject& aOther);
  98.     inline LispInt operator==(LispObject& aOther);
  99.     inline LispInt operator!=(LispObject& aOther);
  100.     inline void SetFlag(LispInt aFlag);
  101.     inline void ResetFlag(LispInt aFlag);
  102.  
  103. #ifdef DEBUG_MODE
  104.     LispCharPtr iFileName;
  105.     LispInt iLine;
  106.     
  107.     inline void SetFileAndLine(LispCharPtr aFileName, LispInt aLine)
  108.     {
  109.         iFileName = aFileName;
  110.         iLine = aLine;
  111.     }
  112. #endif
  113.  
  114. protected:
  115.     inline LispObject()
  116.     {
  117. #ifdef YACAS_DEBUG
  118.         IncNrObjects();
  119. #endif
  120.  
  121. #ifdef DEBUG_MODE
  122.         iFileName = NULL;
  123.         iLine = 0;
  124. #endif
  125.     }
  126.     
  127. private:
  128.     LispPtr   iNext;
  129. };
  130.  
  131.  
  132. /**
  133.  * class LispIterator works almost like LispPtr, but doesn't enforce
  134.  * reference counting, so it should be slightly faster. This one
  135.  * should be used in stead of LispPtr if you are going to traverse
  136.  * a lisp expression in a non-destructive way.
  137.  */
  138. class LispIterator  : public YacasBase
  139. {
  140. public:
  141.     LispIterator(LispPtr& aPtr);
  142.     LispIterator& operator=(LispPtr& aPtr);
  143.     inline LispObject* operator()();
  144.     inline LispPtr* Ptr();
  145.     inline void GoNext();
  146.     inline void GoSub();
  147. private:
  148.     LispPtr* iPtr;
  149. };
  150.  
  151.  
  152. #include "lispobject.inl"
  153.  
  154. #endif
  155.  
  156.  
  157.  
  158.